package it.unibo.refolding.pico.mfp;

import it.unimi.dsi.fastutil.bytes.ByteList;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

abstract class WaveHeader {

  final short type;
  final ByteList unsignedBname;
  final ByteList unsignedDataUnits;
  final int npnts;
  final int unsignedCreationDate;
  final int unsignedModDate;
  final short fsValid;
  final double topFullScale, botFullScale;

  protected WaveHeader(short type, ByteList unsignedBname, ByteList unsignedDataUnits, int npnts,
    int unsignedCreationDate, int unsignedModDate, short fsValid, double topFullScale,
    double botFullScale) {
    this.type = type;
    this.unsignedBname = unsignedBname;
    this.unsignedDataUnits = unsignedDataUnits;
    this.npnts = npnts;
    this.unsignedCreationDate = unsignedCreationDate;
    this.unsignedModDate = unsignedModDate;
    this.fsValid = fsValid;
    this.topFullScale = topFullScale;
    this.botFullScale = botFullScale;
  }

  public boolean equalsCommonFields(WaveHeader other) {
    return type == other.type && unsignedBname.equals(other.unsignedBname)
      && unsignedDataUnits.equals(other.unsignedDataUnits) && npnts == other.npnts
      && unsignedCreationDate == other.unsignedCreationDate
      && unsignedModDate == other.unsignedModDate && fsValid == other.fsValid
      && topFullScale == other.topFullScale && botFullScale == other.botFullScale;
  }

  public String dataUnits() {
    return IgorUtil.unsignedBytesToString(unsignedDataUnits);
  }

  public String bname() {
    return IgorUtil.unsignedBytesToString(unsignedBname);
  }

  public long modDate() {
    return IgorUtil.unsignedIntToLong(unsignedModDate);
  }

  public long creationDate() {
    return IgorUtil.unsignedIntToLong(unsignedCreationDate);
  }

  public abstract WaveHeader withBname(String bname);

  @Override
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj);
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
  }
}
